package com.itheima.itheima_health.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.itheima_health.constant.MessageConstant;
import com.itheima.itheima_health.constant.RedisConstant;
import com.itheima.itheima_health.entity.PageResult;
import com.itheima.itheima_health.entity.QueryPageBean;
import com.itheima.itheima_health.exception.BaseException;
import com.itheima.itheima_health.mapper.CheckMenuMapper;
import com.itheima.itheima_health.pojo.Menu;
import com.itheima.itheima_health.pojo.Setmeal;
import com.itheima.itheima_health.service.CheckMenuService;
import com.itheima.itheima_health.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckMenuServiceImpl implements CheckMenuService {
    @Autowired
    private CheckMenuMapper checkMenuMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private QiniuUtils qiniuUtils;
    @Override
    public PageResult MenuPageQuery(QueryPageBean queryPageBean) {
        //开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //查询套餐数据
        Page<Setmeal> page = checkMenuMapper.MenuPageQuery(queryPageBean);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Transactional
    @Override
    public void MenuAdd(Setmeal setmeal,Integer[] checkgroupIds) {
        try {
            //添加套餐数据xml获取生成的id
            checkMenuMapper.MenuAdd(setmeal);
            //获取套餐id
            Integer MenuId = setmeal.getId();
            //判断检查组ids是否为空
            if (checkgroupIds!=null && checkgroupIds.length>0){
                checkMenuMapper.AddMenuOfthechcheckgroup(checkgroupIds,MenuId);
            }

            //将保存后的img缓存到redis
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
        }catch (Exception e){
            //qiniuUtils.deleteFileFromQiniu(setmeal.getImg());
            if (redisTemplate.opsForSet().isMember(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg())) {
                redisTemplate.opsForSet().remove(RedisConstant.SETMEAL_PIC_DB_RESOURCES, setmeal.getImg());
            }
            throw new BaseException(MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = checkMenuMapper.findById(id);
        return setmeal;
    }

    @Override
    public Integer[] findSetmealOftheCheckgroup(Integer id) {
        Integer[] checkgroupIds = checkMenuMapper.findSetmealOftheCheckgroup(id);
        return checkgroupIds;
    }
    @Transactional
    @Override
    public void delete(Integer id) {
        //查询该检套餐是否有检查项 有则不可删除
        Integer[] list = checkMenuMapper.findSetmealOftheCheckgroup(id);

        if (list.length>0 && list!=null){
            throw new BaseException(MessageConstant.DELETE_SETMEAL_FAIL);
        }else {
            //删除套餐
            checkMenuMapper.delete(id);
        }
    }

    @Transactional
    @Override
    public void update(Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            Integer setmealId = setmeal.getId();
            //删除套餐的检查组
            checkMenuMapper.deleteSetmealOfthecheckgroup(setmealId);

            if (checkgroupIds.length>0 && checkgroupIds!=null){
                //新增套餐的检查组
                checkMenuMapper.AddMenuOfthechcheckgroup(checkgroupIds,setmealId);
            }

            //编辑套餐
            checkMenuMapper.update(setmeal);
        }catch (Exception e){
            throw new BaseException(MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }
}
